package net.expectx.pay.merchant.controller;

import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseResult;
import net.expectx.pay.dao.bean.*;
import net.expectx.pay.merchant.common.AdminConstants;
import net.expectx.pay.rpc.api.MerchantPaymentConfigService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/paymentConfig")
public class PaymentConfigController {

    @Autowired
    private MerchantPaymentConfigService merchantPaymentConfigService;


    @ApiOperation(value = "支付配置主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MerchantUser merchantUser = (MerchantUser) session.getAttribute("merchantUser");

        MerchantPaymentConfigExample merchantPaymentConfigExample = new MerchantPaymentConfigExample();
        MerchantPaymentConfigExample.Criteria criteria = merchantPaymentConfigExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andMerchantNoEqualTo(merchantUser.getMerchantNo());
        List<MerchantPaymentConfig> merchantPaymentConfigs = merchantPaymentConfigService.selectByExample(merchantPaymentConfigExample);
        modelMap.put("merchantPaymentConfigs",merchantPaymentConfigs);
        return "paymentConfig/paymentConfig";
    }

    @ApiOperation(value = "支付配置提交")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, String appid) {
        MerchantPaymentConfigExample merchantPaymentConfigExample = new MerchantPaymentConfigExample();
        MerchantPaymentConfigExample.Criteria criteria = merchantPaymentConfigExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        MerchantPaymentConfig merchantPaymentConfig = merchantPaymentConfigService.selectByPrimaryKey(id);
        merchantPaymentConfig.setAppId(appid);
        return merchantPaymentConfigService.updateByPrimaryKey(merchantPaymentConfig);
    }



}
